package com.eclipse.training.swt.helloworld.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

import com.eclipse.training.swt.helloworld.HelloWorld;
import com.eclipse.training.swt.helloworld.ImageRegistry;
import com.eclipse.training.swt.helloworld.Messages;

public class LeftComposite extends Composite {

	private Text text;
	private Label backGround;
	private TabFolder tabFolder;
	private long sleepTime = 100;

	public LeftComposite(Composite parent, int style) {
		super(parent, style);

		/*
		 * Set the layout
		 */
		setLayout(new GridLayout(3, false));
		setLayoutData(new GridData(GridData.FILL_BOTH));

		/*
		 * Create a group with a row layout and radio buttons
		 */
		Group group = new Group(this, SWT.SHADOW_ETCHED_IN);

		RowLayout layout = new RowLayout(SWT.HORIZONTAL);
		layout.wrap = true;
		layout.fill = true;
		layout.justify = false;
		group.setLayout(layout);

		GridData groupLayoutData = new GridData(GridData.FILL_HORIZONTAL);
		groupLayoutData.horizontalSpan = 3;
		group.setLayoutData(groupLayoutData);

		group.setText(Messages.getString("LeftComposite.0")); //$NON-NLS-1$

		for (int i = 0; i < 10; i++) {
			Button radio = new Button(group, SWT.RADIO);
			radio.setText(Messages.getString("LeftComposite.1") + i); //$NON-NLS-1$
			radio.setLayoutData(new RowData());
			radio.addListener(SWT.Selection, new Listener() {

				@Override
				public void handleEvent(Event event) {
					text.setText(((Button) event.widget).getText());
				}
			});
		}

		/*
		 * Create some widgets
		 */
		Label label = new Label(this, SWT.NONE);
		label.setText(Messages.getString("LeftComposite.2")); //$NON-NLS-1$

		text = new Text(this, SWT.BORDER);

		GridData textLayoutData = new GridData(GridData.FILL_HORIZONTAL);
		textLayoutData.minimumWidth = 300;
		textLayoutData.widthHint = 300;
		text.setMessage(Messages.getString("LeftComposite.3")); //$NON-NLS-1$
		text.setLayoutData(textLayoutData);
		text.setToolTipText(Messages.getString("LeftComposite.4")); //$NON-NLS-1$
		text.addVerifyListener(new VerifyListener() {

			@Override
			public void verifyText(VerifyEvent e) {
				if (e.character == ' ')
					e.doit = false;
				else
					e.doit = true;
			}
		});

		Combo combo = new Combo(this, SWT.READ_ONLY);
		combo.setItems(new String[] {
				Messages.getString("LeftComposite.5"), Messages.getString("LeftComposite.6"), Messages.getString("LeftComposite.7") }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		combo.addListener(SWT.Selection, new Listener() {

			@Override
			public void handleEvent(Event event) {
				text.setText(((Combo) event.widget).getText());
			}
		});

		backGround = new Label(this, SWT.RIGHT | SWT.BORDER);
		backGround.setText(Messages.getString("LeftComposite.8")); //$NON-NLS-1$		
		backGround.setToolTipText(Messages.getString("LeftComposite.9")); //$NON-NLS-1$

		GridData labelLayoutData = new GridData(GridData.FILL_HORIZONTAL);
		labelLayoutData.horizontalSpan = 2;
		backGround.setLayoutData(labelLayoutData);

		combo = new Combo(this, SWT.READ_ONLY);
		combo.setItems(new String[] { "100", "500", "1000", "2000" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		combo.select(0);
		combo.addListener(SWT.Selection, new Listener() {

			@Override
			public void handleEvent(Event event) {
				sleepTime = Integer.valueOf(((Combo) event.widget).getText());
			}
		});

		/*
		 * Create tab folder
		 */
		createTabs(this);
	}

	private void createTabs(Composite parent) {
		/*
		 * Create the tab container
		 */
		tabFolder = new TabFolder(parent, SWT.NONE);
		GridData labelLayoutData = new GridData(GridData.FILL_BOTH);
		labelLayoutData.horizontalSpan = 3;
		tabFolder.setLayoutData(labelLayoutData);

		/*
		 * Add tab items and set their content
		 */
		TabItem one = new TabItem(tabFolder, SWT.NONE);
		one.setText(Messages.getString("LeftComposite.14")); //$NON-NLS-1$
		one.setToolTipText(Messages.getString("LeftComposite.15")); //$NON-NLS-1$
		one.setImage(ImageRegistry.getInstance().getImage(
				ImageRegistry.IMAGE_TAB));
		one.setControl(getTabOneControl(tabFolder));

		TabItem two = new TabItem(tabFolder, SWT.NONE);
		two.setText(Messages.getString("LeftComposite.16")); //$NON-NLS-1$
		two.setToolTipText(Messages.getString("LeftComposite.17")); //$NON-NLS-1$
		two.setImage(ImageRegistry.getInstance().getImage(
				ImageRegistry.IMAGE_TAB));
		two.setControl(getTabTwoControl(tabFolder));

		/*
		 * Select first tab
		 */
		tabFolder.setSelection(0);

		/*
		 * Add a tab selection listener
		 */
		tabFolder.addListener(SWT.Selection, new Listener() {

			@Override
			public void handleEvent(Event arg0) {
				HelloWorld.getStatusLine().setText(
						tabFolder.getSelection()[0].getText()
								+ Messages.getString("LeftComposite.18")); //$NON-NLS-1$
			}
		});
	}

	private Control getTabOneControl(TabFolder tabFolder) {
		/*
		 * First tab contains a scrolled composite
		 */
		final ScrolledComposite sc = new ScrolledComposite(tabFolder, SWT.NONE
				| SWT.V_SCROLL | SWT.H_SCROLL);
		sc.setLayout(new GridLayout());
		sc.setLayoutData(new GridData(GridData.FILL_BOTH));

		/*
		 * Create a child composite to hold the controls
		 */
		final Composite child = new Composite(sc, SWT.NONE);
		child.setLayout(new GridLayout(3, true));

		/*
		 * Each entry in the scrolled composite contains a label and two buttons
		 */
		for (int i = 0; i < 20; i++) {
			final Label label = new Label(child, SWT.NONE);
			label.setText(Messages.getString("LeftComposite.19")); //$NON-NLS-1$
			label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

			/*
			 * Create the buttons
			 */
			Button buttonFont = new Button(child, SWT.PUSH);
			buttonFont.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

			Button buttonColor = new Button(child, SWT.PUSH);
			buttonColor.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			buttonFont.setText(Messages.getString("LeftComposite.20")); //$NON-NLS-1$
			buttonColor.setText(Messages.getString("LeftComposite.21")); //$NON-NLS-1$

			buttonFont.addListener(SWT.Selection, new Listener() {

				@Override
				public void handleEvent(Event arg0) {
					/*
					 * Open the font dialog
					 */
					FontDialog fd = new FontDialog(getShell(), SWT.NONE);
					fd.setText(Messages.getString("LeftComposite.22")); //$NON-NLS-1$
					fd.setRGB(new RGB(0, 0, 255));
					FontData defaultFont = new FontData(Messages
							.getString("LeftComposite.23"), 10, SWT.BOLD); //$NON-NLS-1$
					fd.setFontList(new FontData[] { defaultFont });
					FontData newFont = fd.open();
					if (newFont == null)
						return;
					label.setFont(new Font(getShell().getDisplay(), newFont));
					sc.setMinSize(child.computeSize(SWT.DEFAULT, SWT.DEFAULT));
					sc.layout(true);
				}
			});
			buttonColor.addListener(SWT.Selection, new Listener() {
				private Color color;

				@Override
				public void handleEvent(Event arg0) {

					/*
					 * Open the color dialog
					 */
					ColorDialog dlg = new ColorDialog(getShell());
					dlg.setRGB(label.getForeground().getRGB());
					dlg.setText(Messages.getString("LeftComposite.24")); //$NON-NLS-1$

					RGB rgb = dlg.open();
					if (rgb != null) {
						/*
						 * Dispose the old color, create the new one, and set
						 * into the label
						 */
						if (color != null)
							color.dispose();
						color = new Color(getShell().getDisplay(), rgb);
						label.setForeground(color);
					}
				}
			});
		}

		/*
		 * Set the child as the scrolled content of the ScrolledComposite
		 */
		sc.setContent(child);

		/*
		 * Set the minimum size
		 */
		sc.setMinSize(child.computeSize(SWT.DEFAULT, SWT.DEFAULT));

		/*
		 * Expand both horizontally and vertically
		 */
		sc.setExpandHorizontal(true);
		sc.setExpandVertical(true);

		return sc;
	}

	private Control getTabTwoControl(TabFolder tabFolder) {
		Composite composite = new Composite(tabFolder, SWT.NONE);
		composite.setLayout(new GridLayout());
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));

		/*
		 * Create a canvas to draw an image
		 */
		Canvas canvas = new Canvas(composite, SWT.BORDER | SWT.DOUBLE_BUFFERED);
		canvas.setBackground(getShell().getDisplay().getSystemColor(
				SWT.COLOR_WHITE));
		canvas.setLayoutData(new GridData(GridData.FILL_BOTH));

		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				/*
				 * Scale the image to fit the parent canvas size
				 */
				Image image = ImageRegistry.getInstance().getImage(
						ImageRegistry.IMAGE_TAB_2);
				ImageData imageData = image.getImageData().scaledTo(e.width,
						e.height);

				/*
				 * Remember to dispose the image in order to prevent handler
				 * exception!
				 */
				Image scaled = new Image(getShell().getDisplay(), imageData);
				e.gc.drawImage(scaled, 0, 0);
				scaled.dispose();

			}
		});

		return composite;
	}

	public long getSleepTime() {
		return sleepTime;
	}

	public void updateLabel(int count) {
		backGround.setText(String.valueOf(count++));
	}

	public Control getTabFolder() {
		return this.tabFolder;
	}

}
